package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    private final WmSensitiveMapper wmSensitiveMapper;

    /**
     * 查询敏感词列表 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitiveByPage(SensitiveDto dto) {
        // 1、检查参数
        if (dto == null || dto.getPage() < 0 || dto.getSize() <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 2、分页查询
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        List<WmSensitive> wmSensitives = wmSensitiveMapper.listSensitiveByPage((page - 1) * size, size);
        return ResponseResult.okResult(wmSensitives);
    }


    /**
     * 根据 敏感词id(主键) 删除 敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitiveById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(id<0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveMapper.deleteSensitiveById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 保存敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        // 1、检查参数
        if(adSensitive==null|| StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2、查询该敏感词是否已经存在
        AdSensitive adSensitive1=wmSensitiveMapper.selectBySensitives(adSensitive.getSensitives());
        if(adSensitive1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        adSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.saveSensitive(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        // 1、检查参数
        if(adSensitive==null||adSensitive.getId()==null|| StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive1=wmSensitiveMapper.selectBySensitives(adSensitive.getSensitives());
        if(adSensitive1!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }

        wmSensitiveMapper.updateSensitive(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
